﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SCP_UNA_MVC.Models;
using SCP_UNA_MVC.Libs;

namespace SCP_UNA_MVC.Controllers
{
    public class LocalidadeController : DefaultController
    {
        //
        // GET: /Localidade/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Estado()
        {
            using (SCP_DataDataContext dt = new SCP_DataDataContext())
            {
                var query = dt.Estados.ToList();

                return Json(new { id = query.Select(q => q.estadoid), descricao = query.Select(q => q.descricao) }, JsonRequestBehavior.AllowGet);
            }
        }

        public ActionResult Cidade(string id)
        {
            using (SCP_DataDataContext dt = new SCP_DataDataContext())
            {
                if (!String.IsNullOrEmpty(id))
                {
                    var query = dt.Cidades.Where(q => q.estadoid == Int32.Parse(id)).ToList();

                    return Json(new { id = query.Select(q => q.cidadeid), descricao = query.Select(q => q.descricao) }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(null);
                }
            }
        }
    }
}
